import edu.umd.fosite.geom.Surface;

public class Player extends Thread {

	double moveSpeed = 5;
	double jumpSpeed = 40;
	double flysp = 6;
	double posx;
	double posy;
	double posz;
	double incrz = 0;
	double incrx = 0;
	double a;
	double b;
	double c;
	double d;
	double jump = 0;
	private double veiwRotx;
	private double veiwRoty;
	double fly = 1;
	int height = 6;
	double[] acc = new double[] { 0, -9.8 * 15, 0 };
	double[] vel = new double[] { 0, 0, 0 };

	public Player(int i, int j, int k) {
		posx = i;
		posy = j;
		posz = k;
	}

	public void update(double dt, Surface s) {
		a = Math.sin((double) (veiwRotx) / 360 * 2 * Math.PI);
		b = Math.cos((double) (veiwRotx) / 360 * 2 * Math.PI);
		c = Math.sin((double) (veiwRoty) / 360 * 2 * Math.PI);
		d = Math.cos((double) (veiwRoty) / 360 * 2 * Math.PI);

		if (fly == 0) {

			vel[0] = (a * incrz + b * incrx) * moveSpeed;
			vel[1] += acc[1] * dt;
			vel[2] = (a * incrx - b * incrz) * moveSpeed;

			posx += vel[0] * dt;
			posz += vel[2] * dt; // 866-867-8630
			double h = s.at(posx, posz) + 10;
			if (posy + vel[1] * dt <= h) {
				posy = h;
				vel[1] = 0;
				if (jump == 1) {
					vel[1] = jumpSpeed;

					posy += vel[1] * dt;
				}
				jump = 0;
			} else
				posy += vel[1] * dt;

		} else {

			posx += d * a * incrz * flysp * dt;
			posy += c * incrz * flysp * dt;
			posz -= d * b * incrz * flysp * dt;
			posx += b * incrx * flysp * dt;
			posz += a * incrx * flysp * dt;
		}

	}

	// Getters and Setters
	public double getVeiwRotx() {
		return veiwRotx;
	}

	public void setViewRotx(double veiwRotx) {
		this.veiwRotx = veiwRotx % 360;
	}

	public double getVeiwRoty() {
		return veiwRoty;
	}

	public void setViewRoty(double veiwRoty) {
		if (veiwRoty > 89)
			veiwRoty = 89;
		if (veiwRoty < -89)
			veiwRoty = -89;
		this.veiwRoty = veiwRoty;
	}

}
